home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 60 / IOPROG_60.ISO / soft / c++ / gsl-1.1.1-setup.exe / {app} / src / rng / mt.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-11-01  |  5.5 KB  |  200 lines

  1. /* This program is free software; you can redistribute it and/or
  2.    modify it under the terms of the GNU General Public License as
  3.    published by the Free Software Foundation; either version 2 of the
  4.    License, or (at your option) any later version.
  5.  
  6.    This program is distributed in the hope that it will be useful, but
  7.    WITHOUT ANY WARRANTY; without even the implied warranty of
  8.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  9.    General Public License for more details.  You should have received
  10.    a copy of the GNU General Public License along with this program;
  11.    if not, write to the Free Foundation, Inc., 59 Temple Place, Suite
  12.    330, Boston, MA 02111-1307 USA
  13.  
  14.    Original implementation was copyright (C) 1997 Makoto Matsumoto and
  15.    Takuji Nishimura. Coded by Takuji Nishimura, considering the
  16.    suggestions by Topher Cooper and Marc Rieffel in July-Aug. 1997, "A
  17.    C-program for MT19937: Integer version (1998/4/6)"
  18.  
  19.    This implementation copyright (C) 1998 Brian Gough. I reorganized
  20.    the code to use the module framework of GSL.  The license on this
  21.    implementation was changed from LGPL to GPL, following paragraph 3
  22.    of the LGPL, version 2.
  23.  
  24.    The seeding procedure has been updated to match the 10/99 release
  25.    of MT19937.
  26.  
  27.    The original code included the comment: "When you use this, send an
  28.    email to: matumoto@math.keio.ac.jp with an appropriate reference to
  29.    your work".
  30.  
  31.    Makoto Matsumoto has a web page with more information about the
  32.    generator, http://www.math.keio.ac.jp/~matumoto/emt.html. 
  33.  
  34.    The paper below has details of the algorithm.
  35.  
  36.    From: Makoto Matsumoto and Takuji Nishimura, "Mersenne Twister: A
  37.    623-dimensionally equidistributerd uniform pseudorandom number
  38.    generator". ACM Transactions on Modeling and Computer Simulation,
  39.    Vol. 8, No. 1 (Jan. 1998), Pages 3-30
  40.  
  41.    You can obtain the paper directly from Makoto Matsumoto's web page.
  42.  
  43.    The period of this generator is 2^{19937} - 1.
  44.  
  45. */
  46.  
  47. #include <config.h>
  48. #include <stdlib.h>
  49. #include <gsl/gsl_rng.h>
  50.  
  51. static inline unsigned long int mt_get (void *vstate);
  52. static double mt_get_double (void *vstate);
  53. static void mt_set (void *state, unsigned long int s);
  54.  
  55. #define N 624    /* Period parameters */
  56. #define M 397
  57.  
  58. /* most significant w-r bits */
  59. static const unsigned long UPPER_MASK = 0x80000000UL;    
  60.  
  61. /* least significant r bits */
  62. static const unsigned long LOWER_MASK = 0x7fffffffUL;    
  63.  
  64. typedef struct
  65.   {
  66.     unsigned long mt[N];
  67.     int mti;
  68.   }
  69. mt_state_t;
  70.  
  71. static inline unsigned long
  72. mt_get (void *vstate)
  73. {
  74.   mt_state_t *state = (mt_state_t *) vstate;
  75.  
  76.   unsigned long k ;
  77.   unsigned long int *const mt = state->mt;
  78.  
  79. #define MAGIC(y) (((y)&0x1) ? 0x9908b0dfUL : 0)
  80.  
  81.   if (state->mti >= N)
  82.     {    /* generate N words at one time */
  83.       int kk;
  84.  
  85.       for (kk = 0; kk < N - M; kk++)
  86.     {
  87.       unsigned long y = (mt[kk] & UPPER_MASK) | (mt[kk + 1] & LOWER_MASK);
  88.       mt[kk] = mt[kk + M] ^ (y >> 1) ^ MAGIC(y);
  89.     }
  90.       for (; kk < N - 1; kk++)
  91.     {
  92.       unsigned long y = (mt[kk] & UPPER_MASK) | (mt[kk + 1] & LOWER_MASK);
  93.       mt[kk] = mt[kk + (M - N)] ^ (y >> 1) ^ MAGIC(y);
  94.     }
  95.  
  96.       {
  97.     unsigned long y = (mt[N - 1] & UPPER_MASK) | (mt[0] & LOWER_MASK);
  98.     mt[N - 1] = mt[M - 1] ^ (y >> 1) ^ MAGIC(y);
  99.       }
  100.  
  101.       state->mti = 0;
  102.     }
  103.  
  104.   /* Tempering */
  105.   
  106.   k = mt[state->mti];
  107.   k ^= (k >> 11);
  108.   k ^= (k << 7) & 0x9d2c5680UL;
  109.   k ^= (k << 15) & 0xefc60000UL;
  110.   k ^= (k >> 18);
  111.  
  112.   state->mti++;
  113.  
  114.   return k;
  115. }
  116.  
  117. static double
  118. mt_get_double (void * vstate)
  119. {
  120.   return mt_get (vstate) / 4294967296.0 ;
  121. }
  122.  
  123. static void
  124. mt_set (void *vstate, unsigned long int s)
  125. {
  126.   mt_state_t *state = (mt_state_t *) vstate;
  127.   int i;
  128.  
  129.   if (s == 0)
  130.     s = 4357;    /* the default seed is 4357 */
  131.  
  132.   /* This is the October 1999 version of the seeding procedure. It
  133.      was updated by the original developers to avoid the periodicity
  134.      in the simple congruence originally used.
  135.  
  136.      Note that an ANSI-C unsigned long integer arithmetic is
  137.      automatically modulo 2^32 (or a higher power of two), so we can
  138.      safely ignore overflow. */
  139.  
  140. #define LCG(x) ((69069 * x) + 1) &0xffffffffUL
  141.  
  142.   for (i = 0; i < N; i++)
  143.     {
  144.       state->mt[i] = s & 0xffff0000UL;
  145.       s = LCG(s);
  146.       state->mt[i] |= (s &0xffff0000UL) >> 16;
  147.       s = LCG(s);
  148.     }
  149.  
  150.   state->mti = i;
  151. }
  152.  
  153. /* This is the original version of the seeding procedure, no longer
  154.    used but available for compatibility with the original MT19937. */
  155.  
  156. static void
  157. mt_1998_set (void *vstate, unsigned long int s)
  158. {
  159.   mt_state_t *state = (mt_state_t *) vstate;
  160.   int i;
  161.  
  162.   if (s == 0)
  163.     s = 4357;    /* the default seed is 4357 */
  164.  
  165.   state->mt[0] = s & 0xffffffffUL;
  166.  
  167. #define LCG1998(n) ((69069 * n) & 0xffffffffUL)
  168.  
  169.   for (i = 1; i < N; i++)
  170.     state->mt[i] = LCG1998 (state->mt[i - 1]);
  171.  
  172.   state->mti = i;
  173. }
  174.  
  175. static const gsl_rng_type mt_type =
  176. {"mt19937",            /* name */
  177.  0xffffffffUL,            /* RAND_MAX  */
  178.  0,                    /* RAND_MIN  */
  179.  sizeof (mt_state_t),
  180.  &mt_set,
  181.  &mt_get,
  182.  &mt_get_double};
  183.  
  184. static const gsl_rng_type mt_1998_type =
  185. {"mt19937_1998",        /* name */
  186.  0xffffffffUL,            /* RAND_MAX  */
  187.  0,                    /* RAND_MIN  */
  188.  sizeof (mt_state_t),
  189.  &mt_1998_set,
  190.  &mt_get,
  191.  &mt_get_double};
  192.  
  193. const gsl_rng_type *gsl_rng_mt19937 = &mt_type;
  194. const gsl_rng_type *gsl_rng_mt19937_1998 = &mt_1998_type;
  195.  
  196. /* MT19937 is the default generator, so define that here too */
  197.  
  198. const gsl_rng_type *gsl_rng_default = &mt_type;
  199. unsigned long int gsl_rng_default_seed = 0;
  200.